//结算单生成

interface IfeeAddSettleViewModel extends ng.IScope{
    /**
     * 承运商名称
     */
    carrier:string;
    /**
     * 承运商编号
     */
    carrierId:string;
    /**
     * 承运商号码
     */
    carrierPhone:string;
    /**
     * 总金额
     */
    totalMoney:number;
    /**
     * 开户银行
     */
    bankName:string;
    /**
     * 银行卡号
     */
    bankCode:string;
    /**
     * 持卡人
     */
    holder:string;
    /**
     * 应付说明
     */
    description:string;
    /**
     * 备注
     */
    remarks:string;
    /**
     * 费用列表
     */
    feeList:string[];
    /**
     * 涉及费用条数
     */
    feeLength:number;
    /**
     * 费用code数据
     */
    feeCodeList:string;
    /**
     * 银行卡名称Disable
     */
    bankDisable:boolean;
    /**
     * 承运商自动提示消息
     */
    onCarrierAutoComplate(value:string):any;
    /**
     * 银行卡自动提示消息
     */
    onBankAutoComplate(value:string):any;
    /**
     * 选中承运商自动填充数据
     */
    onCarrierSelected(select:angucomplete<CarrierListItemResponse>):void;
    /**
     * 选中银行卡自动填充数据
     */
    onBankSelected(select:angucomplete<BankListItemResponse>):void;
    /**
     * 生成结算单
     */
    addFeeSettleBillEv():void;
    /**
     * 获取开户银行
     */
    getBankNameInfo():void;
    /**
     * 涉及费用条数
     */
    feeCount:string;
}

class feeAddSettleController {
    /**
     * 费用id(list)
     */
    feeIdList:string;
    constructor(private $scope:IfeeAddSettleViewModel,private feeService:IFeeService,private carrierService:ICarrierService,private bankService:IBankService,private $state:angular.ui.IStateService, private $location:ng.ILocationService, private $ngBootbox:BootboxService,private commonServoce:ICommonService){
        this.feeIdList = this.$location.search().feeIdList;
        this.$scope.feeCodeList = this.$location.search().codeList;
        this.$scope.bankDisable = true;
        this.init();
    }
    init():void{
        this.getCreateBillData();
        this.$scope.feeCount =  this.$location.search().length;
        //输入提示
        this.$scope.onCarrierAutoComplate = this.onCarrierAutoComplate;
        this.$scope.onBankAutoComplate = this.onBankAutoComplate;
        //选中后callback
        this.$scope.onBankSelected = this.onBankSelected;
        this.$scope.onCarrierSelected = this.onCarrierSelected;
        //添加Event
        this.$scope.addFeeSettleBillEv = this.addFeeSettleBillEv;

        this.$scope.getBankNameInfo = this.commonServoce.debounce(this.getBankNameInfo,500);
    }
    /**
     * 获取结算总信息
     */
    getCreateBillData():void{
        this.feeService.createBill(this.feeIdList).then(result=>{
            let sc = this.$scope;
            sc.carrier = result.data.carrier;
            sc.carrierId = result.data.carrierId;
            sc.carrierPhone = result.data.carrierPhone;
            sc.bankName = result.data.bankName;
            sc.bankCode = result.data.bankCode;
            sc.holder = result.data.holder;
            sc.totalMoney = result.data.totalMoney;
        });
    }

    /**
     * 承运商提示
     */
    onCarrierAutoComplate=(value:string)=>{
        return this.carrierService.getList("",true,value,"","","","","",true,0,5).then(res=>{
            return res.data.data;
        });
    }
    /**
     * 银行卡信息提示
     */
    onBankAutoComplate=(value:string)=>{
        return this.bankService.getList("",this.$scope.carrierId,value,false,0,5).then(res=>{
            return res.data.data;
        })
    }

    /**
     * 承运商自动填充信息
     */
    onCarrierSelected=(select:angucomplete<CarrierListItemResponse>)=>{
        if(select){
            this.$scope.carrierId = select.description.id;
            this.carrierService.getDetail(select.description.id).then(res=>{
                this.$scope.carrierPhone = res.data.phone;
                this.$scope.carrier = res.data.name;
            });
        }
    }

    /**
     * 银行卡自动填充
     */
    onBankSelected=(select:angucomplete<BankListItemResponse>)=>{
        if(select){
            this.$scope.bankName = select.description.bank;
            this.$scope.holder = select.description.holder;
            this.$scope.bankCode = select.description.cardNumber;
            $("#bankCode_value").val(select.description.cardNumber);
        }
    }


    /**
     *获取银行卡名称 
     */
    getBankNameInfo=()=>{
        let len = $("#bankCode_value").val().length;
        if(len > 15){
            this.bankService.getBankName($("#bankCode_value").val()).then(res=>{
                if(res.data == ""|| !res.data){ this.$scope.bankDisable = false}else{
                    this.$scope.bankName = res.data;
                    this.$scope.bankDisable = true;
                }
            },ref=>{this.$scope.bankDisable = false; this.$scope.bankName = "";});
        }
    }

    /**
     * 生成结算单
     */
    addFeeSettleBillEv = () => {
        // console.log(this.$scope.totalMoney)
        if(this.$scope.carrierPhone.length&&this.$scope.carrierPhone.length!=11){
            this.$ngBootbox.alert('请输入正确的承运商号码');
            return;
        }
        this.$ngBootbox.confirm("确定生成结算单吗?").then(()=>{
            this.feeService.addFeeSettleBill(
                $("#carrier_value").val(),
                this.$scope.carrierId,
                this.$scope.carrierPhone,
                this.$scope.totalMoney,
                this.$scope.bankName,
                // this.$scope.bankCode,
                $("#bankCode_value").val(),
                this.$scope.holder,
                this.$scope.description,
                this.$scope.remarks,
                this.feeIdList.split(",")
            ).then(res=>{this.$state.go("app.fee.feeCheckList");});
        });
    }
}
angular.module("tms").controller("feeAddSettleController",["$scope","feeService","carrierService","bankService","$state","$location","$ngBootbox","commonService",feeAddSettleController])

